public class SOLOFinanceActionsExt {
	public SOLOFinanceActionsExt(apexPages.standardController stdCtrlr)
	{
		currOpp = (Opportunity)stdCtrlr.getRecord();
	}
	
	private Opportunity currOpp;

	public PageReference processFinanceAction()
	{
		string returnUrl = '';
		string oppId = '';
		string paymentStatus = '';
		string attuid = '';
		string soloUrl = '';
		string officeCode = '';
		
		PageReference pr = new PageReference('/' + currOpp.Id);
		Map<string,string> parameters = apexPages.currentPage().getParameters();
		
		Map<id,userrole> roleMap = new Map<id,userrole> ([SELECT id, name FROM UserRole WHERE id=:UserInfo.getUserRoleId()]);
		//soloUrl = 'http://local.samples.yellowpages.com';
		soloUrl = 'https://' + AppEndPoints__c.getInstance(UserInfo.getProfileId()).SoloDomain__c;
		system.debug(soloUrl);
		string defaultReturnUrl = ListViewIds__c.getInstance(UserInfo.getProfileId()).Opportunity_List_View__c;
		if(roleMap.containsKey(UserInfo.getUserRoleId()))
		{
			string roleName = roleMap.get(UserInfo.getUserRoleId()).Name;
		    if(roleName == 'SFA Finance Admin')
		    {
		    	defaultReturnUrl = ListViewIds__c.getInstance(UserInfo.getProfileId()).SFA_OOR_Finance_Admin_Default__c;
		    }
		    else if(roleName == 'SFA Finance Analyst')
		    {
		    	defaultReturnUrl = ListViewIds__c.getInstance(UserInfo.getProfileId()).SFA_OOR_Finance_Analyst_Default__c;
		    }
		}
		
		returnUrl = EncodingUtil.urlEncode(URL.getSalesForceBaseURL().toExternalForm() + '/' + defaultReturnUrl, 'UTF-8');  // /006 Opportunity List View id in my local sandbox
		system.debug(returnUrl);
		oppId = currOpp.Id;
		
		if(parameters.containsKey('ATTUID'))
		{
			attuid = parameters.get('ATTUID');
		}
		if(parameters.containsKey('loc'))
		{
			officeCode = parameters.get('loc');
		}
		
		if(parameters.containsKey('Action'))
		{
			string strAction = parameters.get('Action');
			if(strAction == 'accept')
			{
				if(parameters.containsKey('UserId'))
				{
					currOpp.OwnerId = parameters.get('UserId');
					update currOpp;
				}
			}
			else if(strAction == 'save')
			{
				//Update SOLO_Billing_Modified_Date in SFDC
				currOpp.SOLO_Billing_Modified_Date__c = DateTime.Now();
				update currOpp;
				
				paymentStatus = 'PI';
										
				//Process this action in SOLO
				pr = new PageReference(soloUrl + '/queues/Process_SFDC_Finance_Action.aspx?' + 
					'ReturnUrl=' + returnUrl + '&' + 'OppId=' + oppId + '&' + 'PmtStatus=' + paymentStatus + '&' + 'ATTUID=' + ATTUID + '&' + 'loc=' + officeCode);
				
			}
			else if(strAction == 'approve' || strAction== 'deny')
			{
				//Update SOLO_Billing_Modified_Date and SOLO_Order_State in SFDC
				currOpp.SOLO_Billing_Modified_Date__c = DateTime.Now();
				currOpp.SOLO_Order_State__c = 'FR';
				currOpp.OwnerId = currOpp.Account.OwnerId;
				update currOpp;

				if(strAction == 'approve')
				{
					paymentStatus = 'AI';
				}
				else
				{
					paymentStatus = 'DI';
				}
								
				//Process this action in SOLO
				pr = new PageReference(soloUrl + '/queues/Process_SFDC_Finance_Action.aspx?' + 
					'ReturnUrl=' + returnUrl + '&' + 'OppId=' + oppId + '&' + 'PmtStatus=' + paymentStatus + '&' + 'ATTUID=' + ATTUID + '&' + 'loc=' + officeCode);
			}
		}
		return pr;
	}

}